package com.haois.mp.database.module.common.daomain;

import com.haois.mp.database.util.ChineseCalendar;
import com.haois.mp.database.util.Lunar;
import com.haois.mp.database.util.jx.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * description
 *
 * @author RuiHaoZ on GuangZhou
 * @date 2018/8/4 14:52
 */
public class RemindDob {
    public RemindDob() {

    }

    public RemindDob(RemindDob remindDob) {
        this.nickName = remindDob.nickName;
        this.id = remindDob.id;
        this.name = remindDob.name;
        this.birthday = remindDob.birthday;
        this.veryLike = remindDob.veryLike;
        this.birthday = remindDob.birthday;
        this.group = remindDob.group;
        this.groupList = remindDob.groupList;
        this.onBirthday = remindDob.onBirthday;
        this.nextBirthday = remindDob.nextBirthday;
        this.age = remindDob.age;
        this.memberNum = remindDob.memberNum;
        this.content = remindDob.content;
        this.updateTime = remindDob.updateTime;
        this.createTime = remindDob.createTime;
        this.groupContent = remindDob.groupContent;
        this.userContent = remindDob.userContent;
    }

    public static final String LUNAR = "旧";
    public static final String SOLAR = "新";
    public static final String YEAR = "Y";
    public static final String DAY = "D";
    public static final String MONTH = "M";
    public static final String WEEK = "W";

    private Integer id;
    private String name;
    private String birthdayType;
    private Integer veryLike;
    private Date birthday;
    private String group;
    private String groupList;
    private String nickName;
    private Date onBirthday;
    private Date nextBirthday;
    private Integer age;
    private Double ageD;
    private String memberNum;
    private String content;
    private String updateTime;
    private String createTime;
    private String userContent;
    private String groupContent;

    public String getUserContent() {
        return userContent;
    }

    public void setUserContent(String userContent) {
        this.userContent = userContent;
    }

    public String getGroupContent() {
        return groupContent;
    }

    public void setGroupContent(String groupContent) {
        this.groupContent = groupContent;
    }

    public Double getAgeD() {
        return ageD;
    }

    public void setAgeD(Double ageD) {
        this.ageD = ageD;
    }

    public Date getNextBirthday() {
        return nextBirthday;
    }

    public void setNextBirthday(Date nextBirthday) {
        this.nextBirthday = nextBirthday;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMemberNum() {
        return memberNum;
    }

    public void setMemberNum(String memberNum) {
        this.memberNum = memberNum;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getOnBirthday() {
        return onBirthday;
    }

    public void setOnBirthday(Date onBirthday) {
        this.onBirthday = onBirthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthdayType() {
        return birthdayType;
    }

    public void setBirthdayType(String birthdayType) {
        this.birthdayType = birthdayType;
    }

    public Integer getVeryLike() {
        return veryLike;
    }

    public void setVeryLike(Integer veryLike) {
        this.veryLike = veryLike;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getGroupList() {
        return groupList;
    }

    public void setGroupList(String groupList) {
        this.groupList = groupList;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "RemindDob{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthdayType='" + birthdayType + '\'' +
                ", veryLike=" + veryLike +
                ", birthday=" + birthday != null ? DateUtil.getYMd(birthday) : "未知" +
                ", group='" + group + '\'' +
                ", onBirthday=" + onBirthday != null ? DateUtil.getYMd(onBirthday) : "未知" +
                ", nextBirthday=" + nextBirthday != null ? DateUtil.getYMd(nextBirthday) : "未知" +
                ", age=" + age +
                ", ageD=" + ageD +
                '}';
    }

    public Date lunarToSolarBirYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(this.birthday);
        return lunarToSolar(calendar);
    }

    public Date lunarToSolarThisYear() {
        Calendar calendar = transferThisYear();
        return lunarToSolar(calendar);
    }

    public Date lunarToSolarNextYear() {
        Calendar calendar = transferNextYear();
//        this.onBirthday = lunarToSolar(calendar);
        return lunarToSolar(calendar);
    }

    public Date lunarToSolarPreviousYear() {
        Calendar calendar = transferPreviousYear();
//        this.onBirthday = lunarToSolar(calendar);
        return lunarToSolar(calendar);
    }

    private Date lunarToSolar(Calendar calendar) {
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH);
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        ChineseCalendar chineseCalendar = new ChineseCalendar(true, y, m + 1, d);
        return DateUtil.stringToDate(chineseCalendar.getSimpleGregorianDateString(), DateUtil.yMd);
    }

    public Date solarToLunar() {
        this.setOnBirthday(this.getBirthday());
        Calendar calendar = transferThisYear();
        Lunar lunar = new Lunar(calendar);
        return DateUtil.stringToDate(lunar.toString(), DateUtil.yMd);
    }

    public void setOnAge() {
        Calendar birthday = Calendar.getInstance();
        Calendar onBirthday = Calendar.getInstance();
        if (this.getBirthdayType().equals(LUNAR)) {
            birthday.setTime(lunarToSolarBirYear());
        } else {
            birthday.setTime(this.birthday);
        }
        onBirthday.setTime(this.onBirthday);
        this.age = onBirthday.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
    }


    public Calendar transferPreviousYear() {
        return transferYear(-1);
    }

    public Calendar transferThisYear() {
        return transferYear(0);
    }

    public Calendar transferNextYear() {
        return transferYear(1);
    }

    public Calendar transferYear(int y) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.setTime(this.birthday);
        calendar.set(year + y, calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        return calendar;
    }

    public static List<RemindDob> filter(List<RemindDob> remindDobs, String onType) {
        List thisDob = new ArrayList();
        Date thisDate = new Date();
        if (RemindDob.DAY.equals(onType)) {
            remindDobs.stream().filter(remindDob ->
                    DateUtil.isThisToday(remindDob.getOnBirthday().getTime())
                            || DateUtil.isThisToday(remindDob.getNextBirthday().getTime()))
                    .forEach(remindDob -> thisDob.add(remindDob));
        } else if (RemindDob.WEEK.equals(onType)) {
            remindDobs.stream().filter(remindDob ->
                    DateUtil.daysBetween(thisDate, remindDob.getOnBirthday()) == 6
                            || DateUtil.daysBetween(thisDate, remindDob.getNextBirthday()) == 6)
                    .forEach(remindDob -> thisDob.add(remindDob));
        } else if (RemindDob.MONTH.equals(onType)) {
            remindDobs.stream().filter(remindDob ->
                    DateUtil.isThisMonth(remindDob.getOnBirthday().getTime())
                            || DateUtil.isThisMonth(remindDob.getNextBirthday().getTime()))
                    .forEach(remindDob -> thisDob.add(remindDob));
        }
        remindDobs.forEach(remindDob -> {
            if (DateUtil.isThisToday(remindDob.getNextBirthday().getTime())
                    || DateUtil.daysBetween(thisDate, remindDob.getNextBirthday()) == 6
                    || DateUtil.isThisMonth(remindDob.getNextBirthday().getTime())) {
                remindDob.onBirthday = remindDob.nextBirthday;
            }
            remindDob.setOnAgeD();
        });
        return thisDob;
    }


    public void setOnAgeD() {
        try {
            Date birthday = this.birthday;
            if (LUNAR.equals(this.getBirthdayType())) {
                birthday = lunarToSolarBirYear();
            }
            double aged = DateUtil.daysBetween(birthday, new Date()) / 365.2;
            BigDecimal b = new BigDecimal(aged);
            double df = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            this.setAgeD(df);
        } catch (Exception e) {

        }

    }
}
